package cn.edu.dhu.acm.oj.judge;

import cn.edu.dhu.acm.oj.judge.thread.*;
import cn.edu.dhu.acm.oj.common.config.Const;

public class MainFrame extends javax.swing.JFrame {

	class Item {

		String name;
		byte index;

		Item(String str, byte i) {
			name = str;
			index = i;
		}

		@Override
		public String toString() {
			return name;
		}

		public byte getIndex() {
			return index;
		}

		public String getName() {
			return name;
		}
	}

	/** Creates new form MainFrame */
	public MainFrame() {
		Control.init(this);
		receiver = new Receiver();
		sender = new Sender();
		thread = new Thread(r);
		//ManJPanel = new ManualJudgePanel();
		initComponents();
		JTA_Response.setText(initResponse);
		JTA_Response.setEditable(false);
		Control.setIsAcceptLocaljudge(CH_AcceptLocal.isSelected());
		for (byte i = 0; i < Const.VERDICT.length; i++) {
			Item item = new Item(Const.VERDICT[i], i);
			JCB_Result.addItem(item);
		}
		JCB_Result.setSelectedIndex(Const.QUEUE);
		JTA_CodeOut.setFont(Const.font);
		JTA_StandardOut.setFont(Const.font);
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jToolBar1 = new javax.swing.JToolBar();
        JB_Apply = new javax.swing.JButton();
        JB_Auto = new javax.swing.JButton();
        CH_AcceptLocal = new javax.swing.JCheckBox();
        jToolBar2 = new javax.swing.JToolBar();
        jLabel1 = new javax.swing.JLabel();
        JT_Gotten = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        JT_SolutionQueue = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        JT_MessageQueue = new javax.swing.JTextField();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jToolBar3 = new javax.swing.JToolBar();
        JB_GetSubmit = new javax.swing.JButton();
        JB_LookInformation = new javax.swing.JButton();
        JB_LookPaper = new javax.swing.JButton();
        JB_Judge = new javax.swing.JButton();
        jToolBar4 = new javax.swing.JToolBar();
        JCB_Result = new javax.swing.JComboBox();
        JB_SendResult = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        JTA_StandardOut = new javax.swing.JTextArea();
        jLabel3 = new javax.swing.JLabel();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        JTA_CodeOut = new javax.swing.JTextArea();
        jLabel4 = new javax.swing.JLabel();
        jPanel5 = new javax.swing.JPanel();
        jToolBar5 = new javax.swing.JToolBar();
        JB_GetQuestion = new javax.swing.JButton();
        jSplitPane1 = new javax.swing.JSplitPane();
        jPanel6 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        jScrollPane4 = new javax.swing.JScrollPane();
        JTA_Question = new javax.swing.JTextArea();
        jPanel7 = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        JTA_Response = new javax.swing.JTextArea();
        jToolBar6 = new javax.swing.JToolBar();
        jTextField1 = new javax.swing.JTextField();
        JB_SendMessage = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("ACMjudge");

        jToolBar1.setFloatable(false);

        JB_Apply.setText("Apply");
        JB_Apply.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JB_ApplyActionPerformed(evt);
            }
        });
        jToolBar1.add(JB_Apply);

        JB_Auto.setText("Start");
        JB_Auto.setEnabled(false);
        JB_Auto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JB_AutoActionPerformed(evt);
            }
        });
        jToolBar1.add(JB_Auto);

        CH_AcceptLocal.setText("AcceptLocalJudge");
        CH_AcceptLocal.setFocusable(false);
        CH_AcceptLocal.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);
        CH_AcceptLocal.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        CH_AcceptLocal.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CH_AcceptLocalActionPerformed(evt);
            }
        });
        jToolBar1.add(CH_AcceptLocal);

        getContentPane().add(jToolBar1, java.awt.BorderLayout.NORTH);

        jToolBar2.setFloatable(false);

        jLabel1.setText("Have gotten :");
        jToolBar2.add(jLabel1);

        JT_Gotten.setEditable(false);
        JT_Gotten.setText("0");
        JT_Gotten.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JT_GottenActionPerformed(evt);
            }
        });
        jToolBar2.add(JT_Gotten);

        jLabel2.setText("Solution queue :");
        jToolBar2.add(jLabel2);

        JT_SolutionQueue.setEditable(false);
        JT_SolutionQueue.setText("0");
        jToolBar2.add(JT_SolutionQueue);

        jLabel7.setText("Message queue:");
        jToolBar2.add(jLabel7);

        JT_MessageQueue.setEditable(false);
        JT_MessageQueue.setText("0");
        JT_MessageQueue.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JT_MessageQueueActionPerformed(evt);
            }
        });
        jToolBar2.add(JT_MessageQueue);

        getContentPane().add(jToolBar2, java.awt.BorderLayout.SOUTH);

        jPanel1.setLayout(new java.awt.BorderLayout());

        jToolBar3.setFloatable(false);

        JB_GetSubmit.setText("GetSubmit");
        JB_GetSubmit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JB_GetSubmitActionPerformed(evt);
            }
        });
        jToolBar3.add(JB_GetSubmit);

        JB_LookInformation.setText("LookInformation");
        JB_LookInformation.setEnabled(false);
        JB_LookInformation.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JB_LookInformationActionPerformed(evt);
            }
        });
        jToolBar3.add(JB_LookInformation);

        JB_LookPaper.setText("LookPaper");
        JB_LookPaper.setEnabled(false);
        JB_LookPaper.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JB_LookPaperActionPerformed(evt);
            }
        });
        jToolBar3.add(JB_LookPaper);

        JB_Judge.setText("Judge");
        JB_Judge.setEnabled(false);
        JB_Judge.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JB_JudgeActionPerformed(evt);
            }
        });
        jToolBar3.add(JB_Judge);

        jPanel1.add(jToolBar3, java.awt.BorderLayout.NORTH);

        jToolBar4.setFloatable(false);
        jToolBar4.add(JCB_Result);

        JB_SendResult.setText("SendResult");
        JB_SendResult.setEnabled(false);
        JB_SendResult.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JB_SendResultActionPerformed(evt);
            }
        });
        jToolBar4.add(JB_SendResult);

        jPanel1.add(jToolBar4, java.awt.BorderLayout.SOUTH);

        jPanel2.setLayout(new java.awt.GridLayout(1, 2));

        jPanel3.setLayout(new java.awt.BorderLayout());

        JTA_StandardOut.setBackground(new java.awt.Color(0, 0, 0));
        JTA_StandardOut.setColumns(20);
        JTA_StandardOut.setForeground(new java.awt.Color(255, 255, 255));
        JTA_StandardOut.setRows(20);
        jScrollPane1.setViewportView(JTA_StandardOut);

        jPanel3.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        jLabel3.setText("StandardOut:");
        jPanel3.add(jLabel3, java.awt.BorderLayout.PAGE_START);

        jPanel2.add(jPanel3);

        jPanel4.setLayout(new java.awt.BorderLayout());

        JTA_CodeOut.setBackground(new java.awt.Color(0, 0, 0));
        JTA_CodeOut.setColumns(20);
        JTA_CodeOut.setForeground(new java.awt.Color(255, 255, 255));
        JTA_CodeOut.setRows(20);
        jScrollPane2.setViewportView(JTA_CodeOut);

        jPanel4.add(jScrollPane2, java.awt.BorderLayout.CENTER);

        jLabel4.setText("CodeOut:");
        jPanel4.add(jLabel4, java.awt.BorderLayout.PAGE_START);

        jPanel2.add(jPanel4);

        jPanel1.add(jPanel2, java.awt.BorderLayout.CENTER);

        jTabbedPane1.addTab("Judge", jPanel1);

        jPanel5.setLayout(new java.awt.BorderLayout());

        jToolBar5.setFloatable(false);
        jToolBar5.setRollover(true);

        JB_GetQuestion.setText("GetQuestion");
        JB_GetQuestion.setFocusable(false);
        JB_GetQuestion.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        JB_GetQuestion.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        JB_GetQuestion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JB_GetQuestionActionPerformed(evt);
            }
        });
        jToolBar5.add(JB_GetQuestion);

        jPanel5.add(jToolBar5, java.awt.BorderLayout.NORTH);

        jSplitPane1.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);

        jPanel6.setLayout(new java.awt.BorderLayout());

        jLabel5.setText("Question:");
        jPanel6.add(jLabel5, java.awt.BorderLayout.CENTER);

        JTA_Question.setColumns(20);
        JTA_Question.setRows(5);
        jScrollPane4.setViewportView(JTA_Question);

        jPanel6.add(jScrollPane4, java.awt.BorderLayout.CENTER);

        jSplitPane1.setTopComponent(jPanel6);

        jPanel7.setLayout(new java.awt.BorderLayout());

        jLabel6.setText("Response:");
        jPanel7.add(jLabel6, java.awt.BorderLayout.PAGE_START);

        JTA_Response.setColumns(20);
        JTA_Response.setRows(5);
        jScrollPane3.setViewportView(JTA_Response);

        jPanel7.add(jScrollPane3, java.awt.BorderLayout.CENTER);

        jSplitPane1.setRightComponent(jPanel7);

        jPanel5.add(jSplitPane1, java.awt.BorderLayout.CENTER);

        jToolBar6.setFloatable(false);
        jToolBar6.setRollover(true);

        jTextField1.setEditable(false);
        jTextField1.setEnabled(false);
        jToolBar6.add(jTextField1);

        JB_SendMessage.setText("SendResponse");
        JB_SendMessage.setFocusable(false);
        JB_SendMessage.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        JB_SendMessage.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        JB_SendMessage.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JB_SendMessageActionPerformed(evt);
            }
        });
        jToolBar6.add(JB_SendMessage);

        jPanel5.add(jToolBar6, java.awt.BorderLayout.SOUTH);

        jTabbedPane1.addTab("Message", jPanel5);

        getContentPane().add(jTabbedPane1, java.awt.BorderLayout.CENTER);

        pack();
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        java.awt.Dimension dialogSize = getSize();
        setLocation((screenSize.width-dialogSize.width)/2,(screenSize.height-dialogSize.height)/2);
    }// </editor-fold>//GEN-END:initComponents

    private void JT_GottenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JT_GottenActionPerformed
}//GEN-LAST:event_JT_GottenActionPerformed

    private void JB_ApplyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JB_ApplyActionPerformed
		Control.setApply();
		JB_Auto.setEnabled(true);
    }//GEN-LAST:event_JB_ApplyActionPerformed

    private void JB_AutoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JB_AutoActionPerformed
		if (JB_Auto.getText().equals("Start")) {
			JB_Auto.setText("Stop");
			Control.setIsauto(true);
			JB_GetSubmit.setEnabled(false);
		} else {
			JB_Auto.setText("Start");
			Control.setIsauto(false);
			JB_GetSubmit.setEnabled(true);
		}
    }//GEN-LAST:event_JB_AutoActionPerformed

    private void JB_GetSubmitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JB_GetSubmitActionPerformed
		try {
			if (Control.GetSubmit()) {
				JB_LookPaper.setEnabled(true);
				JB_LookInformation.setEnabled(true);
				JB_Judge.setEnabled(true);
				JB_SendResult.setEnabled(true);
				JB_GetSubmit.setEnabled(false);
			}
		} catch (Exception e) {
			e.printStackTrace();
			JB_GetSubmit.setEnabled(true);
		}
}//GEN-LAST:event_JB_GetSubmitActionPerformed

    private void JB_LookInformationActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JB_LookInformationActionPerformed
		String str;
		try {
			str = "ContestID : " + Control.getSolutionbean().getContestId();
			str += "\nProblemID : " + Control.getSolutionbean().getProblemId();
			str += "\nUserID : " + Control.getSolutionbean().getUserId();
			str += "\nLanguage : " + Const.LANGUAGE[Control.getSolutionbean().getLanguage()];
			Control.setInfo(str);
			str = Control.getSolutionbean().getSourceCode().getSource();
		} catch (Exception e) {
			str = e.toString();
			JB_GetSubmit.setEnabled(true);
		}
		Control.setCode(str);
}//GEN-LAST:event_JB_LookInformationActionPerformed

    private void JB_LookPaperActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JB_LookPaperActionPerformed
		Control.setProblem();
}//GEN-LAST:event_JB_LookPaperActionPerformed

    private void JB_JudgeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JB_JudgeActionPerformed
		try {
			Control.Judge();
			JTA_StandardOut.setText(Control.getRunbean().getStdAns());
			JTA_CodeOut.setText(Control.getRunbean().getOutput());
			JCB_Result.setSelectedIndex(Control.getRunbean().getResult());
		} catch (Exception e) {
			e.printStackTrace();
			JB_GetSubmit.setEnabled(true);
		}
}//GEN-LAST:event_JB_JudgeActionPerformed

    private void JB_SendResultActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JB_SendResultActionPerformed
		try {
			Item item = (Item) JCB_Result.getSelectedItem();
			Control.getRunbean().setResult(item.getIndex());
			Control.SendResult();
			JTA_StandardOut.setText("");
			JTA_CodeOut.setText("");
			JCB_Result.setSelectedIndex(Const.QUEUE);
			JB_LookPaper.setEnabled(false);
			JB_LookInformation.setEnabled(false);
			JB_Judge.setEnabled(false);
			JB_SendResult.setEnabled(false);
			JB_GetSubmit.setEnabled(true);
		} catch (Exception e) {
			e.printStackTrace();
			JB_GetSubmit.setEnabled(true);
		}
}//GEN-LAST:event_JB_SendResultActionPerformed

    private void CH_AcceptLocalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CH_AcceptLocalActionPerformed
		Control.setIsAcceptLocaljudge(CH_AcceptLocal.isSelected());
}//GEN-LAST:event_CH_AcceptLocalActionPerformed

    private void JT_MessageQueueActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JT_MessageQueueActionPerformed
		// TODO add your handling code here:
}//GEN-LAST:event_JT_MessageQueueActionPerformed

	private void JB_GetQuestionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JB_GetQuestionActionPerformed
		try {
			if (Control.GetMessage()) {
				JB_GetQuestion.setEnabled(false);
				JTA_Response.setEditable(true);
				JB_SendMessage.setEnabled(true);
				JTA_Question.setText(Control.getMessagebean().getQuestion());
			}
		} catch (Exception e) {
			JB_GetQuestion.setEnabled(true);
		}
}//GEN-LAST:event_JB_GetQuestionActionPerformed

    private void JB_SendMessageActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JB_SendMessageActionPerformed
		try {
			String str = JTA_Response.getText();
			if (str.equals("")) {
				str = initResponse;
			}
			Control.getMessagebean().setResponse(str);
			Control.SendMessage();
			JTA_Question.setText("");
			JTA_Response.setText(initResponse);
			JTA_Response.setEditable(false);
			JB_SendMessage.setEnabled(false);
		} catch (Exception e) {
			e.printStackTrace();
		}
		JB_GetQuestion.setEnabled(true);
    }//GEN-LAST:event_JB_SendMessageActionPerformed

	public void StartApply() {
		TReceiver = new Thread(receiver);
		TSender = new Thread(sender);
		TReceiver.start();
		TSender.start();
		thread.start();
	}

	public void setSolutionQueue(int size) {
		JT_SolutionQueue.setText("" + size);
	}

	public void setMessageQueue(int size) {
		JT_MessageQueue.setText("" + size);
	}

	public void setSolutionGotten() {
		solutiongotten++;
		JT_Gotten.setText("" + solutiongotten);
	}

	/**
	 * @param args the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {

			public void run() {
				new MainFrame().setVisible(true);
			}
		});
	}
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox CH_AcceptLocal;
    private javax.swing.JButton JB_Apply;
    private javax.swing.JButton JB_Auto;
    private javax.swing.JButton JB_GetQuestion;
    private javax.swing.JButton JB_GetSubmit;
    private javax.swing.JButton JB_Judge;
    private javax.swing.JButton JB_LookInformation;
    private javax.swing.JButton JB_LookPaper;
    private javax.swing.JButton JB_SendMessage;
    private javax.swing.JButton JB_SendResult;
    private javax.swing.JComboBox JCB_Result;
    private javax.swing.JTextArea JTA_CodeOut;
    private javax.swing.JTextArea JTA_Question;
    private javax.swing.JTextArea JTA_Response;
    private javax.swing.JTextArea JTA_StandardOut;
    private javax.swing.JTextField JT_Gotten;
    private javax.swing.JTextField JT_MessageQueue;
    private javax.swing.JTextField JT_SolutionQueue;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JToolBar jToolBar2;
    private javax.swing.JToolBar jToolBar3;
    private javax.swing.JToolBar jToolBar4;
    private javax.swing.JToolBar jToolBar5;
    private javax.swing.JToolBar jToolBar6;
    // End of variables declaration//GEN-END:variables
	//private ManualJudgePanel ManJPanel;
	private RunAuto r = new RunAuto();
	private Thread thread = new Thread(r);
	private ApplyFrame AppFrame;
	private Thread TReceiver;
	private Thread TSender;
	private Receiver receiver;
	private Sender sender;
	private int solutiongotten = 0;
	private String initResponse = "Sorry! No Response!";
}
